package com.example.demo.controller;

import com.example.demo.common.ApiSign;
import com.example.demo.entity.config.CustomeParam;
import com.example.demo.common.Result;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.TreeMap;

@RestController
@RequestMapping("/sign")
@Api(tags = "12、签名管理")
@ApiSupport(author = "罗海林", order = 12)
@Validated
@RequiredArgsConstructor
public class SignController {

    private final ApiSign apiSign;
    private final CustomeParam param;

    @PostMapping("/createSign")
    @ApiOperation(value = "创建数据签名", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 1)
    public Result createSign() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("userid", "1112331");
        map.put("money", "35.8");

        TreeMap<String, Object> map2 = apiSign.createSign(map);

        Result res = new Result();
        res.ok();
        res.put("userid", "1112331");
        res.put("money", "35.8");
        res.put("nonce", map2.get("nonce").toString());
        res.put("timestamp", map2.get("timestamp").toString());
        res.put("sign", map2.get("sign").toString());
        return res;
    }

    @PostMapping("/checkSign")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户ID"),
            @ApiImplicitParam(name = "money", value = "金额"),
            @ApiImplicitParam(name = "nonce", value = "随机数"),
            @ApiImplicitParam(name = "timestamp", value = "时间差"),
            @ApiImplicitParam(name = "sign", value = "签名")
    })
    @ApiOperation(value = "校验数据签名", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 2)
    public Result checkSign(@RequestParam String userid,
                            @RequestParam String money,
                            @RequestParam String nonce,
                            @RequestParam String timestamp,
                            @RequestParam String sign) {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("key", param.getApiSignKeys());
        map.put("userid", userid);
        map.put("money", money);
        map.put("nonce", nonce);
        map.put("timestamp", timestamp);
        map.put("sign", sign);

        Result res = apiSign.checkSign(map);
        if (res != null) {
            return res;
        }

        res = new Result();
        res.ok();
        return res;
    }

}
